﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MINERP.BLL.PrintManage;
using MINERP.COM;
using System.Runtime.InteropServices;

namespace MINERP.Xray
{
    public partial class FrmPrinter : Form
    {
        public DataSet      _dsPrint = new DataSet();
        //打印的ID
        public string       _printid = string.Empty;

    
        private BLLLogin _bll = new BLLLogin();

        public bool _exit = false;

        public FrmPrinter()
        {
            _bll.GetPrint(out _dsPrint);
            InitializeComponent();
        }

        private void FrmPrinter_Load(object sender, EventArgs e)
        {
            this.lkpPrinters.Properties.DataSource = _dsPrint.Tables[0];
        }

        private void FrmPrinter_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Keys.Enter == (Keys)e.KeyChar)
            {
                Login();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Login();  
        }


        private void Login()
        {
            if (this.lkpPrinters.EditValue != null)
            {

                this._printid = this.lkpPrinters.EditValue.ToString();
                DataRow[] row1 = _dsPrint.Tables[0].Select(string.Format("Printerid={0}", Convert.ToInt32(_printid)));
                int status = Convert.ToInt32(row1[0]["status2"]);
                if (status == 1)
                {
                    if (DialogResult.OK != MessageBox.Show("该打印机已经占用，是否继续使用该打印机？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                    {
                        Control.FromHandle(((FrmOldMain)this.Owner).Handle).Dispose();
                        return;
                    }
                }
                ((FrmOldMain)this.Owner).PrinterId = Convert.ToInt32(_printid);
                //设置打印机的状态为正在使用
                _bll.Login(int.Parse(this._printid));

               //_dsPrint.Tables[0].Rows.Clear();
               //_dsPrint.Tables[0].Rows.Add(row1[0]);

                this.Hide();
                this.Owner.Show();
            }
            else
            {
                MessageBox.Show("请先选择打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //退出登陆
            this._exit = true;
            Logoff.ShowMainFrm();
            this.Owner.Close();
            //this.Owner.Dispose();

        }

        
    }
}
